package com.huawei.esdk.tpoa.business.professional.confmgr;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.datatype.XMLGregorianCalendar;
import com.huawei.esdk.platform.common.MessageContext;
import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.tp.base.BaseService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.Conference;
import com.huawei.esdk.tp.domain.model.PeriodicalConference;
import com.huawei.esdk.tp.domain.model.ScheduleConference;
import com.huawei.esdk.tp.domain.model.SiteInConference;

public class ConfManagerService extends BaseService
{
    public SDKErrorCode delScheduledConf(String confId,
            XMLGregorianCalendar beginTime) throws SDKException
    {  
    	SDKErrorCode errorCode = new SDKErrorCode();
        
//        SDKErrorCode adCheckCode = checkADUserAccess();
//        if(0 != adCheckCode.getErrCode())
//        {
//            errorCode.setErrCode(adCheckCode.getErrCode());
//            return errorCode;
//        }
    	
        if (StringUtils.isEmpty(confId))
        {
            errorCode.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
        }
        else if (!StringUtils.isUintNumber(confId))
        {
            errorCode.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
        }
        else
        {
            Conference conf = new Conference(confId);
            conf.setBeginTime(DateUtils.toDate(beginTime));
            errorCode = conf.deleteConference();
        }
        return errorCode;
    }

    /**
     * 预约普通会议 解耦service，默认值，组合接口，消息校验都在这里处理
     * @throws SDKException 
     */
    public SDKResult<ScheduleConference> scheduleConfResponseService(
            ScheduleConference conference) throws SDKException
    {
    	

        // 不用校验必填，默认值，所有的判断都由产品完成
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        
//        SDKErrorCode adCheckCode = checkADUserAccess();
//        if(0 != adCheckCode.getErrCode())
//        {
//            result.setErrCode(adCheckCode.getErrCode());
//            return result;
//        }
        
        if (!StringUtils.isEmpty(conference.getId())&&!StringUtils.isUintNumber(conference.getId()))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        //增加一个对siteType的校验，当siteType为1，对已siteType_Auto的时候，返回0x22009004
        List<SiteInConference> siteInConferences = conference.getSites();
        for (SiteInConference siteInConference : siteInConferences)
        {
            if (null != siteInConference && ("SITE_TYPE_AUTO").equals(siteInConference.getSiteType()))
            {
                result.setErrCode(ErrInfo.PARAM_IS_RESERVE_ERRORCODE);
                return result;
            }
        }
        
        //设置会议创建者信息和会议删除通知信息
        MessageContext mc = ThreadLocalHolder.get();
        String creatorEmail = (String)mc.getEntities().get("userName");
        conference.setCreatorEmail(creatorEmail);
        
        // 准备Model
        ScheduleConference scheduleConference = new ScheduleConference();
        result = scheduleConference.schedule(conference);
        
        removeExtraSite(conference, result.getResult());
        
        return result;
    }

    /**
     * 修改会议 解耦service，默认值，组合接口，消息校验都在这里处理
     * @throws SDKException 
     */
    public SDKResult<ScheduleConference> editScheduleConfResponseService(
            ScheduleConference conference) throws SDKException
    {
        // 不用校验必填，默认值，所有的判断都由产品完成
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        
//        SDKErrorCode adCheckCode = checkADUserAccess();
//        if(0 != adCheckCode.getErrCode())
//        {
//            result.setErrCode(adCheckCode.getErrCode());
//            return result;
//        }
        
        //增加一个对siteType的校验，当siteType为1，对已siteType_Auto的时候，返回0x22009004
        if (!StringUtils.isUintNumber(conference.getId()))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        List<SiteInConference> siteInConferences = conference.getSites();
        for (SiteInConference siteInConference : siteInConferences)
        {
            if (null != siteInConference && ("SITE_TYPE_AUTO").equals(siteInConference.getSiteType()))
            {
                result.setErrCode(ErrInfo.PARAM_IS_RESERVE_ERRORCODE);
                return result;
            }
        }
        
        //设置会议创建者信息和会议删除通知信息
        MessageContext mc = ThreadLocalHolder.get();
        String creatorEmail = (String)mc.getEntities().get("userName");
        conference.setCreatorEmail(creatorEmail);
        
        // 准备Model
        ScheduleConference scheduleConference = new ScheduleConference();
        result = scheduleConference.reschedule(conference);

        removeExtraSite(conference, result.getResult());
        
        return result;
    }

    // //////////////////////////
    /**
     * 预约普通会议 解耦service，默认值，组合接口，消息校验都在这里处理
     * @throws SDKException 
     */
    public SDKResult<PeriodicalConference> scheduleRecurrenceConfResponseService(
            PeriodicalConference conference) throws SDKException
    {
        // 不用校验必填，默认值，所有的判断都由产品完成
        SDKResult<PeriodicalConference> result = new SDKResult<PeriodicalConference>();

//        SDKErrorCode adCheckCode = checkADUserAccess();
//        if(0 != adCheckCode.getErrCode())
//        {
//            result.setErrCode(adCheckCode.getErrCode());
//            return result;
//        }
        
        if (!StringUtils.isEmpty(conference.getId())&&!StringUtils.isUintNumber(conference.getId()))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        List<SiteInConference> siteInConferences = conference.getSites();
        for (SiteInConference siteInConference : siteInConferences)
        {
            if (null != siteInConference && ("SITE_TYPE_AUTO").equals(siteInConference.getSiteType()))
            {
                result.setErrCode(ErrInfo.PARAM_IS_RESERVE_ERRORCODE);
                return result;
            }
        }
        
        //设置会议创建者信息和会议删除通知信息
        MessageContext mc = ThreadLocalHolder.get();
        String creatorEmail = (String)mc.getEntities().get("userName");
        conference.setCreatorEmail(creatorEmail);
        
        // 准备Model
        PeriodicalConference scheduleConference = new PeriodicalConference();
        result = scheduleConference.scheduleRecurrence(conference);
        
        removeExtraSite(conference, result.getResult());
        
        return result;
    }

    /**
     * 修改会议 解耦service，默认值，组合接口，消息校验都在这里处理
     * @throws SDKException 
     */
    public SDKResult<PeriodicalConference> editScheduleRecurrenceConfResponseService(
            PeriodicalConference conference, Date beginTime) throws SDKException
    {
        // 不用校验必填，默认值，所有的判断都由产品完成
        SDKResult<PeriodicalConference> result = new SDKResult<PeriodicalConference>();
    	
//        SDKErrorCode adCheckCode = checkADUserAccess();
//        if(0 != adCheckCode.getErrCode())
//    	{
//    		result.setErrCode(adCheckCode.getErrCode());
//    		return result;
//    	}
        
        if (!StringUtils.isUintNumber(conference.getId()))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        //增加一个对siteType的校验，当siteType为1，对已siteType_Auto的时候，返回0x22009004
        List<SiteInConference> siteInConferences = conference.getSites();
        for (SiteInConference siteInConference : siteInConferences)
        {
            if (null != siteInConference && ("SITE_TYPE_AUTO").equals(siteInConference.getSiteType()))
            {
                result.setErrCode(ErrInfo.PARAM_IS_RESERVE_ERRORCODE);
                return result;
            }
        }
        //C30修改
        conference.setSubConfStartTime(beginTime);
        
        //设置会议创建者信息和会议删除通知信息
        MessageContext mc = ThreadLocalHolder.get();
        String creatorEmail = (String)mc.getEntities().get("userName");
        conference.setCreatorEmail(creatorEmail);
        
        // 准备Model
        PeriodicalConference scheduleConference = new PeriodicalConference();
        result = scheduleConference.rescheduleRecurrence(conference);

        removeExtraSite(conference, result.getResult());
        return result;
    }

//    private SDKErrorCode checkADUserAccess() throws SDKException
//    {
//    	MessageContext mc = ThreadLocalHolder.get();
//    	String userName = (String)mc.getEntities().get("userName");
//    	String password = (String)mc.getEntities().get("password");
//    	
//    	AuthorizeMode activeDirectory = new AuthorizeMode();
//    	
//    	SDKErrorCode errorCode =
//    	    activeDirectory.verificateADUserAccess(userName, password);
//
//    	return errorCode;
//
//    }
    
    /**
     * 当预约待录播功能的会议时，res录播服务器会把录播服务器当成一个site返回，该方法用于移除这些site
     */
    private void removeExtraSite(ScheduleConference conference,
        ScheduleConference result)
    {
        if(null == conference)
        {
            return;
        }
        
        Map<String, String> originalSites = new HashMap<String, String>();
        for(SiteInConference site : conference.getSites())
        {
            if(StringUtils.isNotEmpty(site.getUri()))
            {
                originalSites.put(site.getUri(), site.getUri());
            }
        }
        
        if(null == result)
        {
            return;
        }
        
        List<SiteInConference> sites = new ArrayList<SiteInConference>();
        for(SiteInConference site : result.getSites())
        {
            if(StringUtils.isEmpty(site.getUri()))
            {
                sites.add(site);
            }
            else if(originalSites.containsKey(site.getUri()))
            {
                sites.add(site);
            }
        }
        result.setSites(sites);
    }
}
